package com.freshcity.page.home
{
	import com.freshcity.common.FriendShowWin;
	import com.freshcity.component.BaseGameObject;
	import com.freshcity.component.container.Component;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.manager.CommunicatorManager;
	import com.freshcity.manager.MsgManager;
	import com.greensock.TweenLite;
	import com.pblabs.debug.Logger;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Dictionary;

	public class FriendListPanel extends BaseGameObject
	{
		public var main:MainScene;

		private var xpos:Number = 0.0;
		private var ypos:Number = 0.0;
		
		override public function destroy():void
		{
			super.destroy();
		}

		override public function initialize():void
		{
			super.initialize();
			
			main = this.owningGroup as MainScene;
			main.commu.register(OpCodes.SYN_FRIEND, onSynFriend);
			main.commu.register(OpCodes.GET_HELLO, onGetHello);
			main.commu.register(OpCodes.SWITCH_ONLINE, onSynOnlineStatus);
			TweenLite.delayedCall(2,loadFriends);
		}
		
		private function loadFriends():void
		{
			main.commu.send(OpCodes.FRIEND_LIST, {}, onInitFriends);
		}
		
		private function onSynFriend(data:*):void
		{
			main.friends[data.id] = data;
		}
		
		private function onSynOnlineStatus(user:*):void
		{
			Logger.debug(this,"user is "+ user.id,";status is " + (user.online != null));
			var s:String = (user.online != null)?"在线":"离线";
			if(user.online != null){
				main.msg.showPopUpMsg("意中人".concat(user.id,"上线啦！"),MsgManager.HELLO,2);
				main.msg.showFixRollMsg("意中人".concat(user.id,"刚刚上线!"));
			}else{
				main.msg.showFixRollMsg("意中人".concat(user.id,"离开了。"));
			}
		}		
		
		private function onGetHello(data:*):void
		{
			main.msg.showPopUpMsg("用户".concat(data,"轻声和你打了一个招呼!"),MsgManager.HELLO,5);
			main.msg.showFixRollMsg("用户".concat(data,"轻声和你打了一个招呼!"));
		}
		
		private function onInitFriends(data:*):void
		{
			for each (var u:Object in data)
			{
				main.friends[u.id] = u;
			}
		}

		public function addFriend(data:*):void
		{
			main.friends[data.id] = data;
			Dispatcher.dispatchEvent(new DataEvent(EventName.AddFollow,data.id));
		}
		
		public function removeFriend(data:*):void
		{
			delete main.friends[data.id];
			Dispatcher.dispatchEvent(new DataEvent(EventName.CancelFollow,data.id));
		}
		
	}
}